﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Redmine.Net.Api.Types;

namespace RedmineWorkerDotNET.App.Classes
{
    class ComparableIdentifiableTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                if (mapDict.ContainsKey(value.ToString()))
                    return mapDict[value.ToString()];
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            object objKey = base.ConvertTo(context, culture, value, destinationType);
            mapDict[objKey.ToString()] = value;
            return objKey;
        }

        private static Dictionary<string, object> mapDict = new Dictionary<string, object>();
    }
}
